package test05;

import java.util.ArrayList;
import java.util.List;

// 1. 抽象组件接口
interface ContentComponent {
    void display();
    int getSize();
    void add(ContentComponent component);
    void remove(ContentComponent component);
}

// 2. 叶子节点 - 文本元素
class TextElement implements ContentComponent {
    private String content;

    public TextElement(String content) {
        this.content = content;
    }

    @Override
    public void display() {
        System.out.println("📝 文本: " + (content.length() > 20 ? content.substring(0, 20) + "..." : content));
    }

    @Override
    public int getSize() {
        return content.length(); // 文字数
    }

    @Override
    public void add(ContentComponent component) {
        throw new UnsupportedOperationException("文本元素不支持添加子项");
    }

    @Override
    public void remove(ContentComponent component) {
        throw new UnsupportedOperationException("文本元素不支持删除子项");
    }
}

// 3. 叶子节点 - 图片元素
class ImageElement implements ContentComponent {
    private String imageUrl;

    public ImageElement(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    @Override
    public void display() {
        System.out.println("🖼️  图片: " + imageUrl);
    }

    @Override
    public int getSize() {
        return 1; // 每张图片计数为1
    }

    @Override
    public void add(ContentComponent component) {
        throw new UnsupportedOperationException("图片元素不支持添加子项");
    }

    @Override
    public void remove(ContentComponent component) {
        throw new UnsupportedOperationException("图片元素不支持删除子项");
    }
}

// 4. 叶子节点 - 视频元素
class VideoElement implements ContentComponent {
    private String videoUrl;
    private int duration; // 视频时长（秒）

    public VideoElement(String videoUrl, int duration) {
        this.videoUrl = videoUrl;
        this.duration = duration;
    }

    @Override
    public void display() {
        System.out.println("🎬 视频: " + videoUrl + " (" + duration + "秒)");
    }

    @Override
    public int getSize() {
        return duration; // 视频时长
    }

    @Override
    public void add(ContentComponent component) {
        throw new UnsupportedOperationException("视频元素不支持添加子项");
    }

    @Override
    public void remove(ContentComponent component) {
        throw new UnsupportedOperationException("视频元素不支持删除子项");
    }
}

// 5. 复合节点 - 笔记
class Note implements ContentComponent {
    private String title;
    List<ContentComponent> children;

    public Note(String title) {
        this.title = title;
        this.children = new ArrayList<>();
    }

    @Override
    public void display() {
        System.out.println("\n📒 笔记: " + title);
        System.out.println("├─ 包含内容:");
        for (ContentComponent child : children) {
            System.out.print("│  ");
            child.display();
        }
    }

    @Override
    public int getSize() {
        int totalSize = 0;
        for (ContentComponent child : children) {
            totalSize += child.getSize();
        }
        return totalSize;
    }

    @Override
    public void add(ContentComponent component) {
        children.add(component);
    }

    @Override
    public void remove(ContentComponent component) {
        children.remove(component);
    }

    public String getTitle() {
        return title;
    }
}

// 6. 复合节点 - 合集
class Collection implements ContentComponent {
    private String name;
    private List<ContentComponent> children;

    public Collection(String name) {
        this.name = name;
        this.children = new ArrayList<>();
    }

    @Override
    public void display() {
        System.out.println("\n📚 合集: " + name);
        System.out.println("├─ 包含内容:");
        for (ContentComponent child : children) {
            System.out.print("│  ");
            child.display();
        }
    }

    @Override
    public int getSize() {
        int totalSize = 0;
        for (ContentComponent child : children) {
            totalSize += child.getSize();
        }
        return totalSize;
    }

    @Override
    public void add(ContentComponent component) {
        children.add(component);
    }

    @Override
    public void remove(ContentComponent component) {
        children.remove(component);
    }

    // 获取合集的总内容量统计
    public void getTotalContentSize() {
        int totalTextSize = 0;
        int totalImages = 0;
        int totalVideoDuration = 0;
        int totalNotes = 0;
        int totalCollections = 0;

        calculateContentStats(this, totalStats -> {
            totalStats[0] = totalTextSize;
            totalStats[1] = totalImages;
            totalStats[2] = totalVideoDuration;
            totalStats[3] = totalNotes;
            totalStats[4] = totalCollections;
        });

        System.out.println("\n=== 合集内容统计 ===");
        System.out.println("总文字数: " + totalTextSize + " 字符");
        System.out.println("总图片数: " + totalImages + " 张");
        System.out.println("总视频时长: " + totalVideoDuration + " 秒");
        System.out.println("总笔记数: " + totalNotes + " 篇");
        System.out.println("总子合集数: " + totalCollections + " 个");
        System.out.println("总体内容量: " + getSize() + " 单位");
    }

    private interface StatsCalculator {
        void calculate(int[] stats);
    }

    private void calculateContentStats(ContentComponent component, StatsCalculator calculator) {
        if (component instanceof TextElement) {
            int[] stats = new int[5];
            calculator.calculate(stats);
            stats[0] += component.getSize();
        } else if (component instanceof ImageElement) {
            int[] stats = new int[5];
            calculator.calculate(stats);
            stats[1] += 1;
        } else if (component instanceof VideoElement) {
            int[] stats = new int[5];
            calculator.calculate(stats);
            stats[2] += component.getSize();
        } else if (component instanceof Note) {
            int[] stats = new int[5];
            calculator.calculate(stats);
            stats[3] += 1;
            Note note = (Note) component;
            for (ContentComponent child : ((Note) component).children) {
                calculateContentStats(child, calculator);
            }
        } else if (component instanceof Collection) {
            int[] stats = new int[5];
            calculator.calculate(stats);
            stats[4] += 1;
            Collection collection = (Collection) component;
            for (ContentComponent child : collection.children) {
                calculateContentStats(child, calculator);
            }
        }
    }
}

// 7. 客户端测试类
public class XiaohongshuContentSystem {
    public static void main(String[] args) {
        System.out.println("=== 小红书笔记内容管理系统 ===\n");

        // 创建内容元素
        TextElement text1 = new TextElement("今天去了一家很棒的咖啡店，环境超级好！");
        TextElement text2 = new TextElement("推荐他们家的拿铁，味道很醇厚。");
        ImageElement image1 = new ImageElement("coffee_shop.jpg");
        ImageElement image2 = new ImageElement("latte.jpg");
        VideoElement video1 = new VideoElement("making_coffee.mp4", 120);

        TextElement text3 = new TextElement("周末去了美术馆，展览很棒！");
        ImageElement image3 = new ImageElement("art_museum.jpg");
        VideoElement video2 = new VideoElement("exhibition_tour.mp4", 180);

        TextElement text4 = new TextElement("分享我的读书笔记...");
        ImageElement image4 = new ImageElement("book_cover.jpg");

        // 创建笔记
        Note coffeeNote = new Note("探店日记：城市角落的咖啡店");
        coffeeNote.add(text1);
        coffeeNote.add(image1);
        coffeeNote.add(text2);
        coffeeNote.add(image2);
        coffeeNote.add(video1);

        Note artNote = new Note("艺术之旅：现代艺术展");
        artNote.add(text3);
        artNote.add(image3);
        artNote.add(video2);

        Note bookNote = new Note("读书分享：设计心理学");
        bookNote.add(text4);
        bookNote.add(image4);

        // 创建子合集
        Collection travelCollection = new Collection("旅行笔记");
        travelCollection.add(coffeeNote);
        travelCollection.add(artNote);

        Collection lifeCollection = new Collection("生活记录");
        lifeCollection.add(bookNote);

        // 创建根合集
        Collection rootCollection = new Collection("我的小红书主页");
        rootCollection.add(travelCollection);
        rootCollection.add(lifeCollection);

        // 演示显示功能
        System.out.println("--- 显示所有内容 ---");
        rootCollection.display();

        // 演示嵌套合集
        System.out.println("\n--- 嵌套合集结构 ---");
        travelCollection.display();

        // 演示单个笔记
        System.out.println("\n--- 单个笔记内容 ---");
        coffeeNote.display();

        // 演示内容量计算
        System.out.println("\n--- 内容量统计 ---");
        System.out.println("咖啡店笔记大小: " + coffeeNote.getSize() + " 单位");
        System.out.println("艺术展笔记大小: " + artNote.getSize() + " 单位");
        System.out.println("旅行合集大小: " + travelCollection.getSize() + " 单位");
        System.out.println("根合集大小: " + rootCollection.getSize() + " 单位");

        // 详细内容统计
        rootCollection.getTotalContentSize();

        // 演示统一处理
        System.out.println("\n--- 统一处理演示 ---");
        ContentComponent[] components = {
                text1,              // 文本元素
                image1,             // 图片元素
                coffeeNote,         // 笔记
                travelCollection,   // 合集
                rootCollection      // 根合集
        };

        for (ContentComponent component : components) {
            System.out.println("\n处理内容项:");
            component.display();
            System.out.println("大小: " + component.getSize() + " 单位");
        }
    }
}